from sklearn.feature_extraction import DictVectorizer


def dict_demo():
    data = [{'city': '北京', 'temperature': 100}, {'city': '上海', 'temperature': 60}, {'city': '深圳', 'temperature': 30}]
    # 1.初始化DictVectorizer对象
    transfer = DictVectorizer(sparse=False)
    # 2.调用fit_transform数值化,返回的稀疏矩阵
    data = transfer.fit_transform(data)

    print(data)
    print(transfer.get_feature_names_out())


if __name__ == '__main__':
    dict_demo()
